//
//  SetShouCangViewController.m
//  MountEverestFM
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "SetShouCangViewController.h"
#import "CollectedModel.h"

@interface SetShouCangViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *likeArr;
@property (nonatomic, strong) UITableView *tabelView;
@property (nonatomic, strong) NSMutableArray *deleteArr;
@property (nonatomic, strong) UIBarButtonItem *righBtn;

@end

@implementation SetShouCangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.righBtn = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnAction:)];
    self.navigationItem.rightBarButtonItem = _righBtn;
    
    self.deleteArr = [NSMutableArray array];
    
    DBHandler *db = [DBHandler shareInstance];
    [db openDB];
    NSArray *tempArr = [db selectAllCollected];
    self.likeArr = [NSMutableArray arrayWithArray:tempArr];
    [db closeDB];
    
    self.tabelView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tabelView.dataSource = self;
    _tabelView.delegate = self;
    [self.view addSubview:_tabelView];
    [_tabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"like"];
    
    if (_likeArr.count == 0) {
        UILabel *noneLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, FM_Screen_Height / 2, FM_Screen_Width, 40 * FM_Screen_Pts_Height)];
        noneLabel.text = @"暂无记录";
        noneLabel.textAlignment = NSTextAlignmentCenter ;
        [self.view addSubview:noneLabel];
    }
}

- (void)rightBtnAction:(UIBarButtonItem *)btn
{
    [self.tabelView setEditing:YES animated:YES];
    _righBtn.title = @"确定";
    [_righBtn setAction:@selector(righBtnWithsure:)];
}

- (void)righBtnWithsure:(UIBarButtonItem *)btn
{
    
    NSLog(@"%@", _deleteArr);
    [_likeArr removeObjectsInArray:_deleteArr];

    DBHandler *db = [DBHandler shareInstance];
    [db openDB];
    for (CollectedModel *collected in _deleteArr) {
        NSLog(@"%@", collected);
        [db deletecollect:collected];
    }
    [db closeDB];
    [_deleteArr removeAllObjects];
    [self.tabelView reloadData];
    
    _righBtn.title = @"删除";
    [_righBtn setAction:@selector(rightBtnAction:)];
    [self.tabelView setEditing:NO animated:NO];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.likeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"like"];
    CollectedModel *collected  = [self.likeArr objectAtIndex:indexPath.row];
    cell.textLabel.text = collected.titelStr;
    NSURL *url = [NSURL URLWithString:collected.image];
    NSLog(@"%@", url);
    cell.imageView.backgroundColor = [UIColor blackColor];
    [cell.imageView sd_setImageWithURL:url];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    //ViewControler 提供了一个可以切换状态的edit按钮
    [super setEditing:editing animated:animated];
    
    //开启编辑状态
    [self.tabelView setEditing:editing animated:animated];
        if (self.editing) {
            self.editButtonItem.title = @"完成";
        }
        else{
            self.editButtonItem.title = @"编辑";
        }
    
}



//添加一项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.righBtn.title isEqualToString:@"确定"]) {
        
          [self.deleteArr addObject:[_likeArr objectAtIndex:indexPath.row]];
        //        NSLog(@"Select---->:%@",self.selectedDic);
    }
}

//取消一项
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.righBtn.title isEqualToString:@"确定"]) {
        [_deleteArr removeObject:[_likeArr objectAtIndex:indexPath.row]];
        //        NSLog(@"Deselect---->:%@",self.selectedDic);
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
